package com.swq.java.code.chapter11.code07;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class GlobalConfig {
    private static final ThreadLocal<GlobalConfig> threadConfig =
            new ThreadLocal<GlobalConfig>();
    private Properties properties = new Properties();

    private GlobalConfig() {
        try {
            //加载配置信息
            InputStream in = getClass().getClassLoader().getResourceAsStream("myapp.properties");
            properties.load(in);
            in.close();
        } catch (IOException e) {
            throw new RuntimeException("加载配置信息失败");
        }
    }

    public static GlobalConfig getInstance() {
        GlobalConfig config = threadConfig.get();
        if (config == null) {
            config = new GlobalConfig();
            threadConfig.set(config);
        }
        return config;
    }

    public Properties getProperties() {
        return properties;
    }

    public static void main(String args[]) {
        GlobalConfig config = getInstance();
        System.out.println(config.getProperties());
    }
}